package com.itheima.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.itheima.dao.BirdMapper;
import com.itheima.po.Bird;
import com.itheima.po.Birdvo;
import com.itheima.service.BirdService;
import com.itheima.service.BirdServiceImpl;

/**
 * 控制器类
 */

public class BirdController implements Controller {
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {

		// BirdService birdService = new BirdServiceImpl();
		ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
		BirdService birdService = act.getBean(BirdService.class);
		List<Bird> birdList = birdService.findBirdAll();
		List<Birdvo> birdvoList = new ArrayList<Birdvo>();
		for (Bird bird : birdList) {
			Birdvo birdvo = new Birdvo();
			// 1.添加bird
			birdvo.setBird(bird);
			// 2.添加了url
			if (bird.getType().equals("凤凰")) {
				birdvo.setUrl(
						"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1721513754,1155659123&fm=26&gp=0.jpg");
			}
			if (bird.getType().equals("乌鸦")) {
				birdvo.setUrl(
						"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.51yuansu.com%2Fpic3%2Fcover%2F00%2F62%2F44%2F5897e6a567c9e_610.jpg&refer=http%3A%2F%2Fpic.51yuansu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621043661&t=3332f832ac234b2a422b7e11910a7664");
			}
			if (bird.getType().equals("乌鸡")) {
				birdvo.setUrl(
						"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.qnong.com.cn%2Fuploadfile%2F2016%2F1114%2F20161114091843465.jpg&refer=http%3A%2F%2Fwww.qnong.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621043700&t=2762e6edc874f9f1d36ca0063eb6df10");
			}
			birdvoList.add(birdvo);
		}

		// 创建ModelAndView对象
		ModelAndView mav = new ModelAndView();

		// 向模型对象中添加数据
		mav.addObject("birdvoList", birdvoList);
		// 设置逻辑视图名
		mav.setViewName("/WEB-INF/jsp/bird.jsp");
		// 返回ModelAndView对象
		return mav;
	}
}
